package com.jrx.anytxn.accounting.controller;

import com.jrx.anytxn.accounting.dto.ManualVoucherRep;
import com.jrx.anytxn.accounting.dto.VoucherInfoRes;
import com.jrx.anytxn.accounting.service.IVoucherInfoService;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @ClassName VoucherInfoController
 * @Description: 传票流水表管理
 * @Author yf
 * @Date 2019/9/9 0009
 * @Version V1.0
 **/
@Api(tags = "传票流水表管理")
@RestController
@RequestMapping("/account/voucherInfo")
public class VoucherInfoController {

    private static final Logger logger = LoggerFactory.getLogger(VoucherInfoController.class);

    @Autowired
    private IVoucherInfoService voucherInfoService;

    @Resource
    private InfoService infoService;

    @ApiOperation("分页查询传票流水表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<VoucherInfoRes>> getAllVoucherInfoByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize
            , @RequestParam(value = "startDate") String startDate, @RequestParam(value = "endDate") String endDate, @RequestParam(value = "loanId") String loanId, @RequestParam(value = "customerId") String customerId,@RequestParam("channel") String channel) {

        TxnRespResult<TxnPage<VoucherInfoRes>> result = new TxnRespResult<>();
        try {
            result.getSuccess(voucherInfoService.getVoucherInfoByPage(pageNum, pageSize,String.valueOf(infoService.getUser().getProjectId()),startDate,endDate,loanId,customerId,channel));
        } catch (TxnException e) {
            logger.error("分页查询传票流水表信息:{}", e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("通过主键查询传票流水表信息")
    @GetMapping("/{id}/{tenantId}/{customerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true)})
    public TxnRespResult<VoucherInfoRes> getVoucherInfoByPrimaryKey(@PathVariable(value = "id") Long id, @PathVariable String tenantId, @PathVariable("customerId") String customerId) {
        TxnRespResult<VoucherInfoRes> result = new TxnRespResult<>();
        try {
            result.getSuccess(voucherInfoService.getVoucherInfoByIdAndTenIdAndCusId(id,tenantId,customerId));
        } catch (TxnException e) {
            logger.error("通过主键查询传票流水表信息:{}", e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("根据科目查询传票流水")
    @GetMapping("/getVoucherInfoBySubjectId/{subjectId}/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", value = "科目编号"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<VoucherInfoRes>> getVoucherInfoBySubjectId(@PathVariable(value = "subjectId") String subjectId,@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<VoucherInfoRes>> result = new TxnRespResult<>();
        TxnPage<VoucherInfoRes> res;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            res = voucherInfoService.getAllPageBySubjectId(subjectId,tenantId,pageNum, pageSize);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("写入传票流水")
    @PostMapping("/createVoucherInfoByList/")
    public TxnRespResult<TxnPage<VoucherInfoRes>> createVoucherInfoByList(@RequestBody ManualVoucherRep voucherInfoRep) {
        TxnRespResult<TxnPage<VoucherInfoRes>> result = new TxnRespResult<>();
        TxnPage<VoucherInfoRes> res;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            String userName = infoService.getUser().getUserName();
            res = voucherInfoService.createVoucherInfoByList(voucherInfoRep.getModeList(),tenantId,userName);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(),e.getMessage());
        }
        return result;
    }
}
